
package com.guigu.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.community.mapper.ProcureMapper;
import com.guigu.community.mapper.ProcureDetailsMapper;
import com.guigu.community.pojo.Procure;
import com.guigu.community.pojo.ProcureDetails;
import com.guigu.community.service.ProcureService;
import com.guigu.community.vo.ProcureVo;
import com.guigu.community.vo.PurchaseRequestVo2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class ProcureServiceImpl extends ServiceImpl<ProcureMapper, Procure> implements ProcureService {

    @Autowired
    private ProcureMapper procureMapper;

    @Autowired
    private ProcureDetailsMapper procureDetailsMapper;

    @Override
    public String queryProcureMaxId() {
        Procure procure = procureMapper.selectProcureMaxId();
        if (procure != null) {
            return procure.getProcureNumber();
        }
        return null;
    }

    @Override
    public boolean submitPurchaseRequest(PurchaseRequestVo2 purchaseRequestvo2) {
        Procure procure = new Procure();
        procure.setProcureCount(purchaseRequestvo2.getTotalQuantity());
        procure.setProcurePrice(purchaseRequestvo2.getTotalAmount());
        // 获取采购人的 ID
        Integer purchaserId = getPurchaserIdByName(purchaseRequestvo2.getPurchaser());
        procure.setPurchasers(purchaserId);
        procure.setProcureRemark(purchaseRequestvo2.getRemark());
        procure.setProcureTime(new Date());
        procure.setProcureTag(1); // 待发货状态
        // 生成采购单编号，这里简单示例，实际需要根据业务规则生成
        String maxId = queryProcureMaxId();
        String newId = "PR" + (maxId == null? "0001" : String.format("%04d", Integer.parseInt(maxId.substring(2)) + 1));
        procure.setProcureNumber(newId);
        // 为 vendor_id 字段赋值，这里假设默认值为 1，你可以根据实际情况修改
        procure.setVendorId(1);
        int result = procureMapper.insert(procure);
        return result > 0;
    }

    @Override
    public boolean submitGoodsList(Integer parentId, List<ProcureVo> procureVoList) {
        for (ProcureVo goods : procureVoList) {
            ProcureDetails details = new ProcureDetails();
            details.setParentId(parentId);
            // 根据实际情况获取商品 ID
            details.setProductId(String.valueOf(getCommodityId(goods.getGoodsName())));
            details.setCount(goods.getQuantity());
            details.setPrice(goods.getUnitPrice());
            details.setSubtotal(goods.getUnitPrice().multiply(BigDecimal.valueOf(goods.getQuantity())));

            // 修改: 为 commodityId 字段赋值
            details.setCommodityId(getCommodityId(goods.getGoodsName()));

            int result = procureDetailsMapper.insert(details);
            if (result <= 0) {
                return false;
            }
        }
        return true;
    }

    // 模拟根据商品名称获取商品 ID 的方法，实际需要根据业务实现
    private Integer getCommodityId(String goodsName) {
        // 这里简单返回 1，实际需要查询数据库获取正确的 ID
        return 1;
    }

    // 模拟根据采购人姓名获取 id 的方法，实际需要根据业务实现
    private Integer getPurchaserIdByName(String purchaserName) {
        // 这里简单返回 1，实际需要查询数据库获取正确的 id
        return 1;
    }

    @Override
    public Procure getProcureByRequest(PurchaseRequestVo2 purchaseRequestVo2) {
        // 根据实际业务逻辑查询采购单
        // 这里假设根据采购单编号查询，需要根据你的实际情况调整
        String maxId = queryProcureMaxId();
        String newId = "PR" + (maxId == null? "0001" : String.format("%04d", Integer.parseInt(maxId.substring(2)) + 1));
        return procureMapper.selectOne(new LambdaQueryWrapper<Procure>()
                .eq(Procure::getProcureNumber, newId));
    }

    @Override
    public PageInfo<Procure> getProcureList(Integer pageNo, Integer pageSize, ProcureVo procureVo) {
        PageHelper.startPage(pageNo, pageSize);
        List<Procure> procureList = procureMapper.selectProcureList(procureVo);
        PageInfo<Procure> procurePageInfo = new PageInfo<>(procureList);
        return procurePageInfo;
    }

    @Override
    public boolean approveProcure(Integer id) {
        Procure procure = new Procure();
        procure.setId(id);
        procure.setCheckTag("S001-1"); // 设置审核通过状态
        return this.updateById(procure);
    }

    @Override
    public boolean rejectProcure(Integer id, String reason) {
        Procure procure = new Procure();
        procure.setId(id);
        procure.setCheckTag("S001-2"); // 设置审核不通过状态
        return this.updateById(procure);
    }

    @Override
    public PageInfo<Procure> getProcureList2(Integer pageNo, Integer pageSize, ProcureVo procureVo) {
        PageHelper.startPage(pageNo, pageSize);
        List<Procure> procureList = procureMapper.selectProcureList2(procureVo);
        PageInfo<Procure> procurePageInfo = new PageInfo<>(procureList);
        return procurePageInfo;
    }
}